package com.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bean.LinkBean;

public class linkfind extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String username = request.getParameter("username");
		System.out.println(username);
		LinkBean bean = new LinkBean();
		bean.setUsername(username);
		try {
			Class.forName("com.mysql.jdbc.Driver");

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String url = "jdbc:mysql://localhost:3306/graduatedesign";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			conn = DriverManager.getConnection(url, "root", "root");
			stmt = conn.createStatement();
			String sql = "select name,sex,age,tel,email from userinfo where username="+ username;
			rs = stmt.executeQuery(sql);
			System.out.println(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (rs.next()) {
				String xingming = rs.getString(1);
				String sex = rs.getString(2);
				String age = rs.getString(3);
				String tel = rs.getString(4);
				String email = rs.getString(5);
				bean.setUsername(username);
				bean.setXingming(xingming);
				bean.setSex(sex);
				bean.setAge(age);
				bean.setTel(tel);
				bean.setEmail(email);
				session.setAttribute("Linkbean", bean);
				response.sendRedirect("admin/linkadd.jsp");
			} else {
				System.out.println("hello");
				response.sendRedirect("admin/notfoundlink.jsp");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
